package com.crane.oss.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crane.common.core.exception.AppException;
import com.crane.common.core.exception.BasicErrorCode;
import com.crane.common.core.exception.SysException;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.common.bean.request.QuerySmsSendRecordReq;
import com.crane.oss.common.bean.request.AddSmsSendRecordReq;
import com.crane.oss.common.bean.request.UpdateSmsSendRecordReq;
import com.crane.oss.common.bean.resp.SendSMSResp;
import com.crane.oss.common.bean.resp.SmsSendRecordResp;
import com.crane.oss.common.dao.SmsSendRecordDao;
import com.crane.oss.common.model.SmsSendRecord;
import com.crane.oss.common.service.SmsSendRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author tyh
 * @email 263998959@qq.com
 * @date 2020年10月13日 11时25分51秒
 */
@Service
@Slf4j
public class SmsSendRecordServiceImpl implements SmsSendRecordService {

    @Resource
    protected SmsSendRecordDao smsSendRecordDao;


    @Override
    public SmsSendRecordResp querySmsSendRecord (Long id) {
        log.info("SmsSendRecordBOImpl querySmsSendRecord id={}", id);
        SmsSendRecord smsSendRecord=smsSendRecordDao.selectById(id);
        if (null == smsSendRecord) {
            throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "smsSendRecord");
        }
        SmsSendRecordResp smsSendRecordResp=new SmsSendRecordResp();
        BeanUtil.copyProperties(smsSendRecord, smsSendRecordResp);
        return smsSendRecordResp;
    }

    @Override
    public IPage<SmsSendRecordResp> queryList(QuerySmsSendRecordReq queryRequest) {
        log.info("SmsSendRecordBOImpl queryList queryRequest={}", queryRequest);
        IPage<SmsSendRecordResp> result = new Page<>();
        result.setCurrent(queryRequest.getPageNo());
        result.setPages(queryRequest.getPageSize());

        LambdaQueryWrapper<SmsSendRecord> querySmsSendRecordWrapper = new LambdaQueryWrapper<>();
        Page<SmsSendRecord> page = new Page<>(queryRequest.getPageNo(), queryRequest.getPageSize());
        IPage<SmsSendRecord> iPage = smsSendRecordDao.
        selectPage(page, querySmsSendRecordWrapper);
        if (CollectionUtils.isEmpty(iPage.getRecords())) {
            result.setTotal(0);
            return result;
        }
        result.setTotal(iPage.getTotal());
        List<SmsSendRecordResp> resultList = BeanUtil.copyList(iPage.getRecords(), SmsSendRecordResp.
        class);
        result.setRecords(resultList);
        return result;
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int save(SendSMSResp sendSMSResp) {
		log.info(" save sendSMSResp={}", sendSMSResp);
		int i = 0;
		if (null != sendSMSResp) {
			List<SendSMSResp> requestList = new ArrayList<SendSMSResp>();
			requestList.add(sendSMSResp);
			i = this.saveBath(requestList);
		}
		return i;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int saveBath(List<SendSMSResp> requestList) {
		log.info(" saveBath requestList={}", requestList);
		int i = 0;
		for (SendSMSResp request : requestList) {
			AddSmsSendRecordReq addRequest = new AddSmsSendRecordReq();
			addRequest.setPhone(request.getPhoneNumber());
			addRequest.setDynamicCode(request.getDynamicCode());
			addRequest.setBizId(request.getBizId());
			addRequest.setCode(request.getResultCode());
			addRequest.setMessage(request.getMessage());
			addRequest.setMsgTypeNo(request.getMsgTypeNo());
			i = this.save(addRequest);
		}
		return i;
	}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(AddSmsSendRecordReq addRequest) {
        log.info("SmsSendRecordServiceImpl save request={}", addRequest);
        Long msgTypeId = 0l;
        SmsSendRecord smsSendRecord =new SmsSendRecord();
        BeanUtil.copyProperties(addRequest, smsSendRecord);
        smsSendRecord.setMsgTypeId(msgTypeId);
        smsSendRecord.setLastSendTime(new Date());
        smsSendRecord.setCreateTime(new Date());
        int i = 0;
        try {
            i =smsSendRecordDao.insert(smsSendRecord);
        } catch (Exception e) {
            log.info("SmsSendRecordBOImpl save e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(UpdateSmsSendRecordReq updateRequest) {
        log.info("SmsSendRecordServiceImpl update request={}", updateRequest);
        SmsSendRecord smsSendRecord=new  SmsSendRecord();
        BeanUtil.copyProperties(updateRequest, smsSendRecord);
        smsSendRecord.setUpdateTime(new Date());
        int i = 0;
        try {
            i =smsSendRecordDao.updateById( smsSendRecord);
        } catch (Exception e) {
            log.info("SmsSendRecordServiceImpl update e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long id) {
        log.info("SmsSendRecordServiceImpl delete id={}", id);
        return smsSendRecordDao.deleteById(id);
    }

}
